package org.dromara.system.controller;

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.WbMinuteFileVo;
import org.dromara.system.domain.bo.WbMinuteFileBo;
import org.dromara.system.service.IWbMinuteFileService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 中标结果附件
 *
 * @author Lion Li
 * @date 2024-05-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/minuteFile")
public class WbMinuteFileController extends BaseController {

    private final IWbMinuteFileService wbMinuteFileService;

    private final ISysOssService ossService;

    /**
     * 查询中标结果附件列表
     */
    @SaCheckPermission("system:minuteFile:list")
    @GetMapping("/list")
    public TableDataInfo<WbMinuteFileVo> list(WbMinuteFileBo bo, PageQuery pageQuery) {
        return wbMinuteFileService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出中标结果附件列表
     */
    @SaCheckPermission("system:minuteFile:export")
    @Log(title = "中标结果附件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WbMinuteFileBo bo, HttpServletResponse response) {
        List<WbMinuteFileVo> list = wbMinuteFileService.queryList(bo);
        ExcelUtil.exportExcel(list, "中标结果附件", WbMinuteFileVo.class, response);
    }

    /**
     * 获取中标结果附件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:minuteFile:query")
    @GetMapping("/{id}")
    public R<WbMinuteFileVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(wbMinuteFileService.queryById(id));
    }

    /**
     * 新增中标结果附件
     */
    @SaCheckPermission("system:minuteFile:add")
    @Log(title = "中标结果附件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbMinuteFileBo bo) {
        return toAjax(wbMinuteFileService.insertByBo(bo));
    }

    /**
     * 修改中标结果附件
     */
    @SaCheckPermission("system:minuteFile:edit")
    @Log(title = "中标结果附件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbMinuteFileBo bo) {
        return toAjax(wbMinuteFileService.updateByBo(bo));
    }

    /**
     * 删除中标结果附件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:minuteFile:remove")
    @Log(title = "中标结果附件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(wbMinuteFileService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 上传OSS对象存储
     *
     * @param file 文件
     */
    @SaCheckPermission("system:minuteFile:edit")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> upload(@RequestPart("file") MultipartFile file, String wbMinuteBidBagId) {
        if (ObjectUtil.isNull(file) && StringUtils.isEmpty(wbMinuteBidBagId)) {
            return R.fail("上传文件不能为空且项目ID不能为空");
        }
        SysOssVo oss = ossService.upload(file);
        WbMinuteFileBo wbMinuteFileBo = new WbMinuteFileBo();
        wbMinuteFileBo.setFileName(oss.getFileName());
        wbMinuteFileBo.setWbMinuteBidBagId(Long.valueOf(wbMinuteBidBagId));
        wbMinuteFileBo.setUrl(oss.getUrl());
        wbMinuteFileBo.setOssId(oss.getOssId());
        wbMinuteFileBo.setFileOriginalName(oss.getOriginalName());
        wbMinuteFileService.insertByBo(wbMinuteFileBo);
        return R.ok();
    }
}
